<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>添加/编辑excel模版</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form label-width="120px" ref="editForm">
          <el-form-item label="excel模版名称：">
            <el-input v-model="name" placeholder="excel模版名称" size="small"></el-input>
          </el-form-item>
          <el-form-item label="上传excel模版：">
            <el-upload
                class="upload-demo"
                action="admin/excel_template/excel"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :before-remove="beforeRemove"
                :on-success="handleAvatarSuccess"
                multiple
                :limit="1"
                :on-exceed="handleExceed"
                :file-list="fileList">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传excel文件，且不超过500kb</div>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click.native="closePOpup" size="small">取消</el-button>
        <el-button size="small" type="primary" @click="checkEditFn">确定保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {excelTemplateAdd, excelTemplateDetail, excelTemplateEdit, excelTemplateUpload} from "@/api/admin/system"

export default {
  props: ['visible', 'modleId'],
  components: {},
  data() {
    return {
      dialogVisible: false,
      name: '',
      file: '',
      fileList: []
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        if (this.modleId) {
          this.getDetailFn()
        }
      } else {
        this.name = ''
        this.file = ''
        this.fileList = []
      }
    }
  },
  created() {
  },
  methods: {
    getList() {
      dutyOptionIndex({p: 0}).then(res => {
        for (let item of res.list) {
          if (!item.parent_id) {
            this.selectList.push(item)
          }
        }
        console.log('res:', res)
      })
    },
    getDetailFn() {
      excelTemplateDetail({id: this.modleId}).then(res => {
        console.log('res:', res)
        this.name = res.vo.name
        this.file = res.vo.file
        this.fileList = [{name: res.vo.name, url: res.vo.file}]
      })
    },
    checkEditFn() {
      console.log('file:', this.file)
      if (!this.name) {
        this.$message.error('excel模版名称');
        return
      }
      if (!this.file) {
        this.$message.error('上传excel模版');
        return
      }
      let data = {
        name: this.name,
        file: this.file
      }
      if (!this.modleId) {
        excelTemplateAdd(data).then(res => {
          this.$emit('InitFn')
          this.closePOpup()
        })
      } else {
        data.id = this.modleId
        excelTemplateEdit(data).then(res => {
          this.$emit('InitFn')
          this.closePOpup()
        })
      }
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleAvatarSuccess(res) {
      this.file = res.data.url
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，已上传了 ${files.length} 个文件，请先删除文件`);
    },
    beforeRemove(file, fileList) {
      console.log('删除：', file, fileList)
      this.file = ''
    }
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 500px;
  height: auto;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.jobTime span {
  padding: 0 10px;
}
</style>


